// pages/weixiu/weixiu.js
import {
  getHouse,
  postRepair,
  getRepairItem
} from '../../api/baoxiu'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    show1: false,
    show2: false,
    actions: [{
      name: '着色选项'
    }, ],
    select: {
      name: '请选择报修房屋'
    },
    select1: {
      name: '请选择维修项目'
    },
    baoxiuArr: [],
    mobile: '',
    date: '选择上门维修日期',
    problem: "",
    fileList: [],
    id: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this
    const eventChannel = this.getOpenerEventChannel()
    if (eventChannel) {
      eventChannel.on('data', function (data) {
        console.log(data);
        that.setData({
          // selectText
          'select.name': data.houseInfo,
          "select.id": data.houseId,
          "select1.name": data.repairItemName,
          "select1.id": data.repairItemId,
          mobile: data.mobile,
          date: data.appointment,
          problem: data.description,
          fileList: data.attachment,
          id:data.id
        })
      })
    }

    wx.getStorage({
      key: 'token',
      async success(resToken) {
        console.log(resToken.data);
        await getHouse({
          token: resToken.data
        }).then(res => {
          console.log(res);
          that.setData({
            actions: res.data.data
          })
        }).catch(res => {
          console.log(res);
        })
        await getRepairItem({
          token: resToken.data
        }).then(res => {
          that.setData({
            baoxiuArr: res.data.data
          })
        }).catch(res => {
          console.log(res);
        })
      },
      fail() {
        wx.redirectTo({
          url: '/pages/login/login',
        })
      }
    })
  },

  showPopup() {
    this.setData({
      show: true
    });
  },

  onClose() {
    this.setData({
      show: false
    });
  },
  onSelect(event) {
    console.log(event);
    this.setData({
      select: event.detail
    })
  },
  showPopup1() {
    this.setData({
      show1: true
    });
  },
  showPopup2() {
    this.setData({
      show2: true
    });
  },
  onClose1() {
    this.setData({
      show1: false
    });
  },
  onSelect1(event) {
    this.setData({
      select1: event.detail
    })
  },
  onDisplay() {
    this.setData({
      show2: true
    });
  },
  onClose2() {
    this.setData({
      show2: false
    });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getMonth() + 1}/${date.getDate()}`;
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
  },
  onConfirm(event) {
    this.setData({
      show2: false,
      date: this.formatDate(event.detail),
    });
  },
  afterRead(event) {
    const {
      file
    } = event.detail;
    const {
      fileList
    } = this.data;
    if (fileList.length >= 9) return wx.showToast({
      title: '问题附件最多为9',
      icon: 'none'
    })
    fileList.push({
      id: fileList.length,
      url: file.url
    });
    console.log(fileList);
    this.setData({
      fileList
    })
  },
  delImg(e) {
    let index = e.detail.index
    console.log(index) //图片在fileList的下标
    let fileList = this.data.fileList;
    fileList.splice(index, 1);
    this.setData({
      fileList
    })

  },
  onChange(e) {
    this.setData({
      mobile: e.detail
    })
    console.log(this.data.mobile);
  },
  onChange1(event) {
    this.setData({
      problem: event.detail
    })
  },
  submit() {
    let data = this.data
    console.log(data.select);
    let obj = {
      houseId: data.select.id,
      repairItemId: data.select1.id,
      mobile: data.mobile,
      appointment: data.date,
      description: data.problem,
      attachment: data.fileList
    }
    if (!obj.houseId) return wx.showToast({
      title: '请选择报修房屋',
      icon: 'none'
    })
    if (!obj.repairItemId) return wx.showToast({
      title: '请选择维修项目',
      icon: 'none'
    })
    if (!this.checkPhone(obj.mobile)) return wx.showToast({
      title: '请输入正确的手机号',
      icon: 'none'
    })
    if (!obj.appointment) return wx.showToast({
      title: '请选择预约日期',
      icon: 'none'
    })
    if (!obj.description) return wx.showToast({
      title: '请输入问题描述',
      icon: 'none'
    })
    if (!obj.attachment) return wx.showToast({
      title: '请上传问题附件',
      icon: 'none'
    })
    if (data.id) obj.id = data.id
    wx.getStorage({
      key: 'token',
      async success(res) {
        await postRepair({
          token: res.data,
          data: obj
        }).then(res => {
          console.log(res);
          if (res.data.code === 10000) {
            wx.showToast({
              title: '提交成功'
            })
            setTimeout(() => {
              wx.navigateBack({
                url: '/pages/index/index.js'
              })
            }, 2000);
          }
        }).catch(res => {
          console.log(res);
        })
      }
    })
  },
  checkPhone(mobile) {
    let RegExp = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
    return RegExp.test(mobile)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})